<template>
    <body id="paper">
    <el-form :model="loginForm" ref="ruleForm" :rules="rules" class="login-container" label-position="left"
             label-width="0px" v-loading="loading">
        <h3 class="login_title">系统登录</h3>
        <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username"
                      auto-complete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password"
                      auto-complete="off" placeholder="密码"></el-input>
        </el-form-item>
        <el-checkbox class="login_remember" v-model="checked"
                     label-position="left"><span style="color: #505458">记住密码</span></el-checkbox>
        <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="submitLogin">登录</el-button>
            <router-link to="register"><el-button type="primary" style="width: 40%;background: #505458;border: none">注册</el-button></router-link>
        </el-form-item>
    </el-form>
    </body>
</template>

<script>
    import {login} from "../api/login";
    import {insertLoginLog} from "../api/log";
    import {getUserInfo} from "../api/users";

    export default {
        name: "Login",
        data () {
            return {
                rules: {
                    username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
                    password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
                },
                checked: true,
                loginForm: {
                    username: '',
                    password: ''
                },
                loading: false,
                userInfo: {}
            }
        },
        methods: {
            async loginUser () {
                const {data} = await login(this.loginForm)
                if (data.code == 200) {
                    console.log(data)
                    this.$message({
                        title: "登录成功",
                        message: data.message,
                        type: "success"
                    })
                    //保存token
                    //localStorage.setItem("JWT_TOKEN", data.data);
                    //console.log(localStorage.getItem('JWT_TOKEN'));
                    //存储用户token
                    const tokenStr = data.data.token.tokenHead +' '+ data.data.token.token;
                    window.localStorage.setItem('JWT_TOKEN', tokenStr);
                    console.log(localStorage.getItem('JWT_TOKEN'));
                    //插入登录日志
                    await insertLoginLog();
                    //跳转到main页面
                    //this.$router.push("/main");
                    this.getUser();
                }else {
                    this.$message.error({
                        title: "登录失败",
                        message: data.message,
                        type: "error"
                    });
                }
            },
            async submitLogin() {
                this.$refs.ruleForm.validate((valid) => {

                    if (valid) {
                        this.loginUser()
                    } else {
                        this.$message.error('请输入所有字段');
                        return false;
                    }
                })
            },
            async getUser() {
                const { data: res } = await getUserInfo();
                if (res.code !== 200)
                    return this.$message.error("获取用户信息失败:" + res.msg);
                this.userInfo = res.data;
                //保存用户
                this.$store.commit("setUserInfo", res.data.user);
                //跳转到main
                this.$router.push("/main");
            }
        }
    }
</script>

<style>
    #paper {
        height: 100%;
        width: 100%;
        background-size: cover;
        position: fixed;
    }
    body{
        margin: 0;
    }
    .login-container {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 90px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .login_title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }
    .login_remember {
        margin: 0px 0px 35px 0px;
        text-align: left;
    }
</style>